'##############
'Electricity Script
'##############

'Controls Bus Electicity and tells the systems
'if the busbar is active or not

'(c) 13.06.2009 Marcel Kuhnt
'(c) 2012		RedGunn

'Script Version: 1.1
'Omsi release: 1.04
'Tedom C12 version: 1.0

'Needs:
'- engine

'Revision History:
'- Marcel Kuhnt		13.06.2009	File Created
'- Rüdiger Hülsmann	23.12.2010	L.L.elec_busbar_avail = 1 by init
'- RedGunn			25.06.2012  Added broken glass

'Attention:
'============
'All voltages are measured without units.
'1.0 means 100% of the normal voltage, 0.0 means no voltage.

'--------------------------------------------------------------

{macro:Elec_Init}
'	At the beginning, battery has enough voltage:
	1.0 (S.L.elec_V_battery) (L.L.elec_busbar_avail)
	
'	Errors	
	1 (S.L.Tlak_pumpa)
	5 (S.L.tlak_pris_ecas)
	0 (S.L.VDO_mode)
{end}

{macro:Elec_Frame}
'	AI elektronika:
	(L.L.AI)
	{if}
		1 (S.L.elec_busbar_avail) (S.L.elec_busbar_main_sw) (S.L.elec_busbar_main)
		0 (S.L.elec_failure_general)
	{endif}
	
'	Porucha dobíjania:
	(L.L.engine_n) (C.L.elec_generator_minRPM) >  (L.L.elec_V_generator) ! &&
	{if}
		4 (S.L.VDO_mode)
	{else}
		1 (S.L.VDO_timer)
		1 (S.L.VDO_porucha_spust)
	{endif}
	
'	Nízke napätie batérie:
	(L.L.elec_V_battery) (C.L.elec_busbar_Vmin) <=  (L.L.elec_busbar_avail) ! &&
	{if}
		3 (S.L.VDO_mode)
	{else}
		1 (S.L.VDO_timer)
		1 (S.L.VDO_porucha_spust)
	{endif}

'	If engine's RPM is high enough, generator will become active:

	(L.L.engine_n) (C.L.elec_generator_minRPM) > (S.L.elec_V_generator)

'	Battery's voltage decrease while main busbar switch is on (but only to zero ;-) ):
	(L.L.elec_busbar_main_sw)
	{if}
		(L.L.elec_V_battery)
		(L.S.Timegap) (C.L.elec_battery_duration) / 
		- 0 max (S.L.elec_V_battery)
	{endif}

'	But battery's voltage instantly grows up to the level of the generator voltage; value will be stored for further steps.
	(L.L.elec_V_battery) (L.L.elec_V_generator) max (S.L.elec_V_battery)

'	If the battery voltage is above minimum and there is no electricity failure, the busbar becomes available
	(C.L.elec_busbar_Vmin) >= (L.L.elec_failure_general) ! &&
	{if}
		1 (S.L.elec_busbar_avail)
	{else}
		0 (S.L.elec_busbar_avail)
	{endif}
	
	(L.L.elec_failure_general) 
	(L.L.velocity) 30 >= &&
	{if}
		1 (S.L.prasknuti)
		(T.L.glass_broken)
	{endif}
	
	(L.L.elec_failure_general) 0 =
	{if}
		0 (S.L.prasknuti)
	{endif}

'	If the battery or generator has voltage and if the switch is on and if there is no electricity failure, the main busbar will become active:
	(L.L.elec_busbar_avail)
	(L.L.elec_busbar_main_sw) &&
	(L.L.elec_failure_general) ! &&
	(S.L.elec_busbar_main)
{end}
